<?php

namespace app\admin\controller\common;

use app\common\controller\AdminBase;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class Common extends AdminBase
{
    public function upload()
    {

    }
    
    // 通用导出
    public function export($data,$header = [])
    {
        $sp = new Spreadsheet();
        $sheet = $sp->getActiveSheet();
        $header = ["顾问名称","分配量","添加量","成交量"];
        foreach ($header as $key => $value) {
            $sheet->setCellValueByColumnAndRow($key + 1,1,$value);
        }
        $row = 2;
        foreach ($data as $key=>$value) {
            $sheet->setCellValueByColumnAndRow(1,$row,$value['real_name']);
            $sheet->setCellValueByColumnAndRow(2,$row,$value['total']['all']);
            $sheet->setCellValueByColumnAndRow(3,$row,$value['total']['add_count']."({$value['total']['add_lv']})");
            $sheet->setCellValueByColumnAndRow(3,$row,$value['total']['order_count']."({$value['total']['order_lv']})");
            $row ++;
        }
        ob_end_clean();
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="统计.xlsx"');
        header('Cache-Control: max-age=0');
        $write = new Xlsx($sp);
        $write->save('php://output');
        $sp->disconnectWorksheets();
        unset($sp);
        exit;
    }
}